﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using ServerBase.LunarSocket;
using ServerBase.Protocol;
using UtilLib;
using static ServerBase.Config.Conf;
using static ServerBase.Service.BaseDispatch;
using static UtilPublic.Redis.ServerRedis;
using ServerGame.Service;
using System.Collections.Generic;
using UtilPublic.ServerLoger;
using ServerBase.Service;
using ServerBase.Config;
using static ServerGame.Service.Dispatcher;
using ServerBase.Client;
using System;
using ServerPublic.Data;
using ServerGame.GameObject;
using static UtilPublic.Serialization.Jsh;
using ServerPublic.DataBaseMysql;
using UtilPublic.ApiDispatcher;
using ServerGame.Data;

namespace ServerGame.Manager
{
    /// <summary>
    /// 管理 技能
    /// </summary>
    public static class SkillMgr
    {
        //技能升级所需潜能
        public static long GetPotLevelup(int skilllv)
        {
            long lv = skilllv + 1;
            return (lv * lv);
        }
        //技能等级 获取
        public static int Skill(this MudChar obj, string SkillId)
        {
            obj.DictSkills.TryGetValue(SkillId, out var lv);
            return lv;
        }
        //技能等级设置
        public static void Skill(this MudChar obj, string SkillId, int lv)
        {
            obj.DictSkills[SkillId] = lv;
        }
        //是否有应用
        public static bool UsageHas(this MudChar obj, EUsageType usage, out string SkillId)
        {
            obj.DictUsages.TryGetValue(usage.ToString(), out SkillId);
            return (SkillId.IsNullOrEmpty() || SkillId == "无");
        }
        //有效等级获取
        public static int UsageSkill(this MudChar obj, string SkillId)
        {
            obj.DictSkills.TryGetValue(SkillId, out var lv);
            if (lv > 0)
            {
                if (Conf.ConfSkill.TryGetValue(SkillId, out var conf))
                {
                    lv = lv * conf.Quotiety / 100;
                }
            }
            return lv;
        }
        //有效等级获取
        public static bool UsageGet(this MudChar obj, EUsageType usage, out string SkillId, out int level)
        {
            level = 0;
            if (obj.DictUsages.TryGetValue(usage.ToString(), out SkillId))
            {
                level += obj.UsageSkill(usage.ToString());
                level += obj.UsageSkill(SkillId);
            }
            else
            {
                SkillId = "";
            }
            return !(SkillId.IsNullOrEmpty() || SkillId == "无");
        }

        public static void GetInfo(this Player player, Dictionary<string, CLS_InfoSkill> info)
        {
            foreach (var kvp in player.DaCommon.DictSkill)
            {
                var node = new CLS_InfoSkill()
                {
                    Id = kvp.Key,
                    Level = kvp.Value,
                };
                info[node.Id] = node;
            }
        }
        public static void GetInfo(this Player player, Dictionary<string, CLS_InfoUsage> info)
        {
            foreach (EUsageType item in Enum.GetValues(typeof(EUsageType)))
            {
                if (player.Skill(item.ToString()) <= 0)
                {
                    continue;
                }
                player.UsageGet(item, out var SkillId, out var level);
                var node = new CLS_InfoUsage()
                {
                    Usage = item.ToString(),
                    Skill = SkillId,
                    Level = level,
                };
                info[node.Usage] = node;
            }
        }
        public static void GetInfo(this Player player, List<CLS_InfoSkill> info)
        {
            foreach (var kvp in player.DaCommon.DictSkill)
            {
                var node = new CLS_InfoSkill()
                {
                    Id = kvp.Key,
                    Level = kvp.Value,
                };
                info.Add(node);
            }
        }
        public static void GetInfo(this Player player, List<CLS_InfoUsage> info)
        {
            foreach (EUsageType item in Enum.GetValues(typeof(EUsageType)))
            {
                if (player.Skill(item.ToString()) <= 0)
                {
                    continue;
                }
                player.UsageGet(item, out var SkillId, out var level);
                var node = new CLS_InfoUsage()
                {
                    Usage = item.ToString(),
                    Skill = SkillId,
                    Level = level,
                };
                info.Add(node);
            }
        }
    }
}
